package com.bsg.upm.check;

import java.text.MessageFormat;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.bsg.upm.constant.Messages;
import com.bsg.upm.entity.DictEntity;
import com.bsg.upm.entity.DictTypeEntity;

/**
 * 字典业务处理检查
 * 
 * @author HCK
 *
 */
@Service
public class DictCheck extends BaseCheck {

	/**
	 * 字典类型项新增时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkSave(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkSaveNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkSaveLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 字典类型项编辑时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkUpdate(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkUpdateNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkUpdateLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 字典类型项删除时检查
	 * 
	 * @param dict
	 *            字典类型项
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkRemove(DictEntity dict) {
		CheckResult chkRS = new CheckResult();
		// 存在性检查
		if (dict == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "字典类型项"));
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 新增时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 字典类型编码非空和长度检查
		String dictTypeCode = (String) paramMap.get("dictTypeCode");
		if (StringUtils.isBlank(dictTypeCode)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "字典类型编码"));
			return chkRS;
		}
		if (StringUtils.trim(dictTypeCode).length() > 32) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "字典类型编码", 32));
			return chkRS;
		}

		// 字典类型项编码非空和长度检查
		String code = (String) paramMap.get("code");
		if (StringUtils.isBlank(code)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "字典类型项编码"));
			return chkRS;
		}
		if (StringUtils.trim(code).length() > 32) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "字典类型项编码", 32));
			return chkRS;
		}

		// 字典类型项名称非空和长度检查
		String name = (String) paramMap.get("name");
		if (StringUtils.isBlank(name)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "字典类型项名称"));
			return chkRS;
		}
		if (StringUtils.trim(name).length() > 255) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "字典类型项名称", 255));
			return chkRS;
		}

		// 顺序非空检查
		Integer sequence = (Integer) paramMap.get("sequence");
		if (sequence == null) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "顺序"));
			return chkRS;
		}

		return chkRS;
	}

	/**
	 * 新增时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 字典类型编码存在性检查
		String dictTypeCode = (String) paramMap.get("dictTypeCode");
		DictTypeEntity dictType = dictTypeDao.get(dictTypeCode);
		if (dictType == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "字典类型编码"));
			return chkRS;
		}

		// 字典类型项编码重复性检查
		String dictCode = (String) paramMap.get("code");
		DictEntity dict = dictDao.getByDictTypeCodeAndDictCode(dictTypeCode, dictCode);
		if (dict != null) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "字典类型项编码"));
			return chkRS;
		}

		// 字典类型项名称重复性检查
		String dictName = (String) paramMap.get("name");
		dict = dictDao.getByDictTypeCodeAndDictName(dictTypeCode, dictName);
		if (dict != null) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "字典类型项名称"));
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 编辑时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkUpdateNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 字典类型编码非空检查
		String dictTypeCode = (String) paramMap.get("dictTypeCode");
		if (StringUtils.isBlank(dictTypeCode)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "字典类型编码"));
			return chkRS;
		}

		// 字典类型项编码非空检查
		String code = (String) paramMap.get("code");
		if (StringUtils.isBlank(code)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "字典类型项编码"));
			return chkRS;
		}

		// 字典类型项名称非空和长度检查
		String name = (String) paramMap.get("name");
		if (StringUtils.isBlank(name)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "字典类型项名称"));
			return chkRS;
		}
		if (StringUtils.trim(name).length() > 255) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "字典类型项名称", 255));
			return chkRS;
		}

		// 顺序非空检查
		Integer sequence = (Integer) paramMap.get("sequence");
		if (sequence == null) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "顺序"));
			return chkRS;
		}

		return chkRS;
	}

	/**
	 * 编辑时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkUpdateLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 字典类型编码存在性检查
		String dictTypeCode = (String) paramMap.get("dictTypeCode");
		DictTypeEntity dictType = dictTypeDao.get(dictTypeCode);
		if (dictType == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "字典类型编码"));
			return chkRS;
		}

		// 字典类型项存在性检查
		String dictCode = (String) paramMap.get("code");
		DictEntity dict = dictDao.getByDictTypeCodeAndDictCode(dictTypeCode, dictCode);
		if (dict == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "字典类型项"));
			return chkRS;
		}

		// 字典类型项名称重复性检查
		String dictName = (String) paramMap.get("name");
		if (!dict.getName().equals(dictName)) {
			dict = dictDao.getByDictTypeCodeAndDictName(dictTypeCode, dictName);
			if (dict != null) {
				chkRS.setStatus(HttpStatus.SC_CONFLICT);
				chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "字典类型项名称"));
				return chkRS;
			}
		}
		return chkRS;
	}
}
